//
//  MainView.m
//  Tip Calculator
//
//  Created by Brennan Stehling on 12/7/08.
//  Copyright SmallSharpTools.com 2008. All rights reserved.
//

#import "MainView.h"

@implementation MainView

- (IBAction)calculate {
    [self doCalculation];
}

- (IBAction)changeRate {
    [self doCalculation];
}

- (void)doCalculation {
	tipRateFloat = [tipRate value];
	tipAmountFloat = [billAmount.text floatValue] * tipRateFloat;
	totalAmountFloat = [billAmount.text floatValue] * (tipRateFloat + 1);
	
	if (tipRateFloat <= 0.1001) {
		ratingOut.text = [NSString stringWithFormat:@"%@ ("@"%.0f%%)", @"OK", tipRateFloat * 100];
	} else if (tipRateFloat <= 0.1501) {
		ratingOut.text = [NSString stringWithFormat:@"%@ ("@"%.0f%%)", @"Average", tipRateFloat * 100];
	} else if (tipRateFloat <= 0.2001) {
		ratingOut.text = [NSString stringWithFormat:@"%@ ("@"%.0f%%)", @"Good", tipRateFloat * 100];
	} else if (tipRateFloat <= 0.2501) {
		ratingOut.text = [NSString stringWithFormat:@"%@ ("@"%.0f%%)", @"Great", tipRateFloat * 100];
	} else {
		ratingOut.text = [NSString stringWithFormat:@"%@ ("@"%.0f%%)", @"Amazing", tipRateFloat * 100];
	}
	
	tipAmountOut.text = [NSString stringWithFormat:@"$"@"%.2f", tipAmountFloat];
	totalAmountOut.text = [NSString stringWithFormat:@"$"@"%.2f", totalAmountFloat];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	if (textField == billAmount) {
		[self doCalculation];
	}
	return YES;
}

- (id)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		// Initialization code
	}
	return self;
}

- (void)drawRect:(CGRect)rect {
	// Drawing code
}

- (void)dealloc {
	[super dealloc];
}

@end
